﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.Generic;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIAM06 : ViewBase
    {
        
        #region ==BDEKEN===========
        public static readonly DependencyProperty BDEKENProperty = DependencyProperty.Register("BDEKEN",
            typeof(string), typeof(UIAM06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBDEKENChanged));
        public string BDEKEN
        {
            get
            {
                return (string)GetValue(BDEKENProperty);
            }
            set
            {
                SetValue(BDEKENProperty, value);
                OnBDEKENChg();
            }
        }
        private static void OnBDEKENChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAM06;
            if (src != null)
            {
                src.BDEKEN = (string)e.NewValue;
            }
        }
        public virtual void OnBDEKENChg()
        { }
        #endregion
        #region ==BDEMOD===========
        public static readonly DependencyProperty BDEMODProperty = DependencyProperty.Register("BDEMOD",
            typeof(string), typeof(UIAM06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBDEMODChanged));
        public string BDEMOD
        {
            get
            {
                return (string)GetValue(BDEMODProperty);
            }
            set
            {
                SetValue(BDEMODProperty, value);
                OnBDEMODChg();
            }
        }
        private static void OnBDEMODChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAM06;
            if (src != null)
            {
                src.BDEMOD = (string)e.NewValue;
            }
        }
        public virtual void OnBDEMODChg()
        { }
        #endregion

        #region ==MODNR===========
        public static readonly DependencyProperty MODNRProperty = DependencyProperty.Register("MODNR",
            typeof(string), typeof(UIAM06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMODNRChanged));
        public string MODNR
        {
            get
            {
                return (string)GetValue(MODNRProperty);
            }
            set
            {
                SetValue(MODNRProperty, value);
                OnMODNRChg();
            }
        }
        private static void OnMODNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAM06;
            if (src != null)
            {
                src.MODNR = (string)e.NewValue;
            }
        }
        public virtual void OnMODNRChg()
        { }
        #endregion
        
        #region ==MESCHL===========
        public static readonly DependencyProperty MESCHLProperty = DependencyProperty.Register("MESCHL",
            typeof(string), typeof(UIAM06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMESCHLChanged));
        public string MESCHL
        {
            get
            {
                return (string)GetValue(MESCHLProperty);
            }
            set
            {
                SetValue(MESCHLProperty, value);
                OnMESCHLChg();
            }
        }
        private static void OnMESCHLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAM06;
            if (src != null)
            {
                src.MESCHL = (string)e.NewValue;
            }
        }
        public virtual void OnMESCHLChg()
        { }
        #endregion
        #region ==EINSTNR===========
        public static readonly DependencyProperty EINSTNRProperty = DependencyProperty.Register("EINSTNR",
            typeof(string), typeof(UIAM06),
            new FrameworkPropertyMetadata("1", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEINSTNRChanged));
        public string EINSTNR
        {
            get
            {
                return (string)GetValue(EINSTNRProperty);
            }
            set
            {
                SetValue(EINSTNRProperty, value);
                OnEINSTNRChg();
            }
        }
        private static void OnEINSTNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAM06;
            if (src != null)
            {
                src.EINSTNR = (string)e.NewValue;
            }
        }
        public virtual void OnEINSTNRChg()
        { }
        #endregion

        #region ==AMEPROGKZ===========
        public static readonly DependencyProperty AMEPROGKZProperty = DependencyProperty.Register("AMEPROGKZ",
            typeof(string), typeof(UIAM06),
            new FrameworkPropertyMetadata("P", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAMEPROGKZChanged));
        public string AMEPROGKZ
        {
            get
            {
                return (string)GetValue(AMEPROGKZProperty);
            }
            set
            {
                SetValue(AMEPROGKZProperty, value);
                OnAMEPROGKZChg();
            }
        }
        private static void OnAMEPROGKZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAM06;
            if (src != null)
            {
                src.AMEPROGKZ = (string)e.NewValue;
            }
        }
        public virtual void OnAMEPROGKZChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.AMEPROGKZ = AMEPROGKZ;
        }
        #endregion

        
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;
            dwData = TBMain.NewRow();
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 4);
            POS = EXAUF.Substring(6, 2);
            MODNR = EXAUF.Substring(8, 3);
            dwData["AMEMANDANT"] = app.Mandant;
            dwData["AMENRKRS"] = "AND AMENRKRS= '" + KRS + "'";
            dwData["AMELFDNR"] = "AND AMELFDNR= '" + LFD + "'";
            dwData["AMEPOS"] = "AND AMEPOS= '" + POS + "'";
            dwData["AMEMODNR"] = "AND AMEMODNR= '" + MODNR + "'";
            SResult = TMV.QAME02(dwData);
            MergeData(SResult, false);
            base.OnEXAUFChg(vlaue);
        }
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            if (KEY.Length < 14)
                return;
            dwData = TMV.TBMain.NewRow();

            dwData["AMEMANDANT"] = app.Mandant;
            dwData["AMENRKRS"] = "AND AMENRKRS= '" + KEY.Substring(0, 2) + "'";
            dwData["AMELFDNR"] = "AND AMELFDNR= '" + KEY.Substring(2, 4) + "'";
            dwData["AMEPOS"] = "AND AMEPOS= '" + KEY.Substring(6, 2) + "'";
            dwData["AMEMODNR"] = "AND AMEMODNR= '" + KEY.Substring(8, 3) + "'";
            dwData["AMEEINSTNR"] = "AND AMEEINSTNR= '" + KEY.Substring(11, 3) + "'";
            SResult = TMV.QAME02(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
        public void CopyMTPCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            QueryCommand(null, null);
            SResult = TMV.QAME04(dwMaster);
            foreach (DataRow dwAM in SResult.Tables[0].Rows)
            {
                dwData = TBMain.NewRow();
                dwData["AMEMANDANT"] = app.Mandant;
                dwData["AMENRKRS"] = dwAM["AMNRKRS"];
                dwData["AMELFDNR"] = dwAM["AMLFDNR"];
                dwData["AMEPOS"] = dwAM["AMPOS"];
                TMV.DAME02(dwData);
            }
            foreach (DataRow row in TBMain.Copy().Rows)
            {
                foreach (DataRow dwAM in SResult.Tables[0].Rows)
                {
                    row["AMENRKRS"] = dwAM["AMNRKRS"];
                    row["AMELFDNR"] = dwAM["AMLFDNR"];
                    row["AMEPOS"] = dwAM["AMPOS"];
                    string BDEKEN = dwAM["AMNRKRS"].ToString() + dwAM["AMLFDNR"].ToString().Substring(2) + row["AMEBDEKEN"].ToString().Substring(4);
                    row["AMEBDEKEN"] = BDEKEN;
                     TMV.InsertAMEINST(row);
                }
            }
        }
      
        public void ResetSeq(object sender, RoutedEventArgs e)
        {
            if (!TMV.IsITX)
            {
                return;
            } 
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                AMEMESCHL = "";

            }
            else
            {
                KEY = TMV.AMENRKRS + TMV.AMELFDNR + TMV.AMEPOS + TMV.AMEMODNR+TMV.AMEEINSTNR;
                AMEMESCHL = TMV.AMEMESCHL; 
            }
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["AMEMANDANT"] = app.Mandant;
            dwData["AMENRKRS"] = "AND AMENRKRS= '" + KRS + "'";
            dwData["AMELFDNR"] = "AND AMELFDNR= '" + LFD + "'";
            dwData["AMEPOS"] = "AND AMEPOS= '" + POS + "'";
            // dwData["AMEMODNR"] = "AND AMEMODNR= '" + ART + "'"; 
            SResult = TMV.QAME02(dwData);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {

            if (!IsNew)
            {
                IsNew = true;
                EINSTNR = "1";
                QTY = 1;
                UPOS = KRS + Helpers.Helper.Right(LFD, 2);
                return;
            }
            if (POS.Trim().Length < 2)
            {
                MessageBox.Show("必须先输入操作类型");
                return;
            }
            PM["NRKRS"] = KRS;
            PM["LFDNR"] = LFD;
            PM["POS"] = POS;
            PM["MODNR"] = MODNR;
            PM["MESCHL"] = MESCHL;
            if (QTY > 1)
            {
                for (int i = 1; i <= QTY; i++)
                {
                    EINSTNR = Helpers.Helper.Right("000" + i.ToString(), 3);
                    PM["EINSTNR"] = EINSTNR;
                    PM["BDEKEN"] = UPOS + Helper.Right(MODNR, 2) + Helper.Right(EINSTNR, 2);
                    PM["MESCHL"] = Helpers.Helper.Left(MESCHL.Trim().PadRight(2), 2);
                    TMV.NAMEINST(PM);
                }
            }
            else
            {
                PM["EINSTNR"] = Helpers.Helper.Right("000" + EINSTNR, 3);  
                PM["BDEKEN"] = (KRS+LFD.Substring(2,2)+MODNR.Substring(1,2)+ Helpers.Helper.Right("000" + EINSTNR,2)).PadRight(8);
                TMV.NAMEINST(PM);
            } 
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            FARBE = "";
            UPOS = "1";
            base.NewCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DAME01(dwMaster);
            filter = "AMENRKRS='" + dwMaster["AMENRKRS"].ToString() + "' AND AMELFDNR='" + dwMaster["AMELFDNR"].ToString() + "' AND AMEPOS='" + dwMaster["AMEPOS"].ToString()
                + "' AND AMEMODNR='" + dwMaster["AMEMODNR"].ToString()
                + "' AND AMEEINSTNR='" + dwMaster["AMEEINSTNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
           foreach (DataRow row in TBMain.Rows)
            {
                TMV.UpdateAMEINST(row, 0);
            }
            base.SaveCommand(sender, e);
        }
    }
    public partial class UIAM06
    {
        public VAMEINST TMV
        {
            get
            {
                return (VAMEINST)MV;
            }
        }
         
        #region ==AMEMESCHL===========
        public static readonly DependencyProperty AMEMESCHLProperty = DependencyProperty.Register("AMEMESCHL",
            typeof(string), typeof(UIAM06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAMEMESCHLChanged));
        public string AMEMESCHL
        {
            get
            {
                return (string)GetValue(AMEMESCHLProperty);
            }
            set
            {
                SetValue(AMEMESCHLProperty, value);
                OnAMEMESCHLChg();
            }
        }
        private static void OnAMEMESCHLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAM06;
            if (src != null)
            {
                src.AMEMESCHL = (string)e.NewValue;
            }
        }
        public virtual void OnAMEMESCHLChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.AMEMESCHL = AMEMESCHL;
        }
        #endregion

    }
}
